#include "connect_setup.h"
#include "ui_connect_setup.h"
#include<QDebug>
Connect_Setup::Connect_Setup(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Connect_Setup)
{
    ui->setupUi(this);
    BaudRate_init();
    DataBits_init();
    CheckBit_init();
    StopBit_init();
    Severip_init();
    Port_init();
    AgreementType_init();
    SerialPort_init();
    serial=new QSerialPort;
    myserver=new MyServer;
    connect(myserver,SIGNAL(send_socketname(QString)),this,SLOT(receive_socketname(QString)));
}

Connect_Setup::~Connect_Setup()
{
    delete ui;
    delete serial;
    delete myserver;
}

void Connect_Setup::BaudRate_init()
{
    //设置波特率
    ui->comboBox_BaudRate->addItem("110");
    ui->comboBox_BaudRate->addItem("300");
    ui->comboBox_BaudRate->addItem("600");
    ui->comboBox_BaudRate->addItem("1200");
    ui->comboBox_BaudRate->addItem("2400");
    ui->comboBox_BaudRate->addItem("4800");
    ui->comboBox_BaudRate->addItem("9600");
    ui->comboBox_BaudRate->addItem("14400");
    ui->comboBox_BaudRate->addItem("19200");
    ui->comboBox_BaudRate->addItem("38400");
    ui->comboBox_BaudRate->addItem("56000");
    ui->comboBox_BaudRate->addItem("57600");
    ui->comboBox_BaudRate->addItem("76800");
    ui->comboBox_BaudRate->setCurrentText("9600");
}

void Connect_Setup::DataBits_init()
{
    //设置数据位
    ui->comboBox_DataBits->addItem("5");
    ui->comboBox_DataBits->addItem("6");
    ui->comboBox_DataBits->addItem("7");
    ui->comboBox_DataBits->addItem("8");
    ui->comboBox_DataBits->setCurrentText("8");
}

void Connect_Setup::CheckBit_init()
{
    //设置校验位
    ui->comboBox_CheckBit->addItem("None");
    ui->comboBox_CheckBit->addItem("Odd");
    ui->comboBox_CheckBit->addItem("Even");
    ui->comboBox_CheckBit->addItem("Mark");
    ui->comboBox_CheckBit->addItem("Space");
    ui->comboBox_CheckBit->setCurrentText("None");
}

void Connect_Setup::StopBit_init()
{
    //设置停止位
    ui->comboBox_StopBit->addItem("1");
    ui->comboBox_StopBit->addItem("2");
    ui->comboBox_StopBit->setCurrentText("1");
}

void Connect_Setup::Severip_init()
{
    //将默认服务器ip设置为本机ip
    ui->comboBox_Severip->setCurrentText("127.0.0.1");
}

void Connect_Setup::Port_init()
{
    ui->lineEdit_port->setText("8888");
}

void Connect_Setup::AgreementType_init()
{
    ui->comboBox_AgreementType->addItem("TCP Server");
    ui->comboBox_AgreementType->addItem("UDP Server");
    ui->comboBox_AgreementType->setCurrentText("TCP Server");
}

void Connect_Setup::SerialPort_init()
{
        //获得所有可用端口列表
        QList<QSerialPortInfo> serialPortInfoList = QSerialPortInfo::availablePorts();
        if(serialPortInfoList.isEmpty()){
            qDebug()<<"没有可使用的串口";
        }
        QList<QSerialPortInfo>::iterator iter = serialPortInfoList.begin();
        //将所有端口添加到界面的下拉列表中
        while(iter!=serialPortInfoList.end()){
            ui->comboBox_SerialPort->addItem(iter->portName());
            iter++;
        }
}

void Connect_Setup::set_init()
{
    serial->setBaudRate(ui->comboBox_BaudRate->currentText().toInt());
    //设置数据位
    if(ui->comboBox_DataBits->currentText()=="5")
        serial->setDataBits(QSerialPort::Data5);
    else if(ui->comboBox_DataBits->currentText()=="6")
        serial->setDataBits(QSerialPort::Data6);
    else if(ui->comboBox_DataBits->currentText()=="7")
        serial->setDataBits(QSerialPort::Data7);
    else
        serial->setDataBits(QSerialPort::Data8);
    //设置校验位
    if(ui->comboBox_CheckBit->currentText()=="None")
        serial->setParity(QSerialPort::NoParity);
    else if(ui->comboBox_CheckBit->currentText()=="Even")
        serial->setParity(QSerialPort::EvenParity);
    else if(ui->comboBox_CheckBit->currentText()=="Odd")
        serial->setParity(QSerialPort::OddParity);
    else if(ui->comboBox_CheckBit->currentText()=="Mark")
        serial->setParity(QSerialPort::MarkParity);
    else
        serial->setParity(QSerialPort::SpaceParity);
    //设置停止位
    if(ui->comboBox_StopBit->currentText()=="1")
        serial->setStopBits(QSerialPort::OneStop);
    else
        serial->setStopBits(QSerialPort::TwoStop);
    //设置流控制
    serial->setFlowControl(QSerialPort::NoFlowControl);
}

QString Connect_Setup::get_comboBox_Clientip()
{
    return ui->comboBox_Clientip->currentText();
}

void Connect_Setup::open_serialport()
{

    if(ui->tabWidget->currentIndex()==0)
    {//判断串口开启状态
        if(serial->isOpen())
        {
            //如果串口已经打开，则关闭它
            serial->clear();
            serial->close();
            qDebug()<<"串口已关闭";
        }
        else
        {
            //若串口没有打开，则打开选择的串口
            serial->setPortName(ui->comboBox_SerialPort->currentText());
            serial->open(QIODevice::ReadWrite);
            set_init();
            qDebug()<<"串口已打开";
        }
    }
    else if(ui->tabWidget->currentIndex()==1)
    {
        if(!myserver->isListening())
        {
            myserver->start_listen(ui->lineEdit_port->text());

        }
        else if(myserver->isListening())
        {
            qDebug()<<"停止监听";
            myserver->close();
        }
    }
    emit send_currentindex(ui->tabWidget->currentIndex());
}

void Connect_Setup::receive_socketname(QString socketname)
{
    ui->comboBox_Clientip->addItem(socketname);
}


